package com.bxait.toymail.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/*
@description:
@ClassName MyMQConfig
@author Chen
@create 2023-06-18 16:08
@Version 1.0
*/
@Configuration
public class MyMQConfig {

    /*
     * @param
     * @==============History===============<br/>
     * @Description //   这是一个延迟队列,订单创建成功后会发了一个消息到这个队列,但是没有队列来监听这个队列,到达过期时间之后,
     *                       这个队列中的消息会转发到order.release.order.queue这个队列
     * @Date 2023/6/18 2023/6/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",60000);
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments);
        return queue;
    }


    /*
     * @param
     * @==============History===============<br/>
     * @Description //   这个队列种的消息最终就是要消费的,主要的业务逻辑就是在监听这个队列
     * @Date 2023/6/18 2023/6/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue orderReleaseOrderQueue() {
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }


    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange",true,false);
    }

    /*
     * @param
     * @==============History===============<br/>
     * @Description //   创建一个绑定关系
     * @Date 2023/6/18 2023/6/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding orderCreateOrderBingDing() {
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);

    }

    @Bean
    public Binding orderReleaseOrderBingDing() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }


    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBingDing() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }



    
    /*
     * @param 
     * @==============History===============<br/>
     * @Description //   秒杀中需要用的队列和路由键
     * @Date 2023/6/23 2023/6/23 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return org.springframework.amqp.core.Queue
     */
    @Bean
    public Queue orderSeckillOrderQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        return new Queue("order.seckill.order.queue",true,false,false);
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding(){
        /**
         * String destination, DestinationType destinationType, String exchange, String routingKey,
         * 			Map<String, Object> arguments
         */
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }
}
